package com.example.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Employee;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import com.example.service.ArticleService;
import com.example.service.EmployeeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController

public class WebController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private AdminService adminService;
    @Resource
    private ArticleService articleService;

    @GetMapping("/hello")
    public Result hello() {
        return Result.success ("hello 奶龙");
    }

    /*
    管理员/员工登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account result = null ;
        if ("ADMIN".equals(account.getRole())) { //管理员登录
            result = adminService.login(account);
        }else if ("EMP".equals(account.getRole())) {
            result = employeeService.login(account);
        }else {
            throw new CustomException("500","非法输入");
        }
        return Result.success(result);
    }

    /*
    员工注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody Employee employee) {
        employeeService.register(employee); //注册不用返回用户信息
        return Result.success();
    }

    /*
    修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if ("ADMIN".equals(account.getRole())) { //管理员登录
            adminService.updatePassword(account);
        }else if ("EMP".equals(account.getRole())) {
            employeeService.updatePassword(account);
        }else {
            throw new CustomException("500","非法输入");
        }
        return Result.success();
    }

    @GetMapping("/barData")
    public Result getBarData() {
        Map<String,Object> map = new HashMap<>();
        List<Employee> employeeList=  employeeService.selectAll(null);
        Set<String> departmentNameSet = employeeList.stream().map(Employee::getDepartmentName).collect(Collectors.toSet()); //去重
        map.put("department",departmentNameSet);  //x轴数据
        List<Long> countList = new ArrayList<>();
        for (String departmentName : departmentNameSet) {
            //统计这个部门下面的员工数量
            Long count = employeeList.stream().filter(employee -> employee.getDepartmentName().equals(departmentName)).count();
            countList.add(count);
        }
        map.put("count",countList);  //y轴员工数量数据
        return Result.success(map);
    }

    @GetMapping("/lineData")
    public Result getLineData() {
        Map<String,Object> map = new HashMap<>();

        Date date = new Date(); //当前时间
        DateTime start = DateUtil.offsetDay(date, -7); //起始日期
        List<DateTime> dateTimeList = DateUtil.rangeToList(start , date , DateField.DAY_OF_YEAR);
        //把DateTime 类型的日期转换为字符串类型日期["10月23日"]
        List<String> dateStrList = dateTimeList.stream().map(dateTime -> DateUtil.format(dateTime, "MM月dd日")) //展示用月日
                .sorted(Comparator.naturalOrder()).collect(Collectors.toList());
        map.put("date",dateStrList);  //x轴数据

        List<Integer> countList = new ArrayList<>();
        for (DateTime day : dateTimeList) {
            //3月11日
            //上面函数中的月日和数据库中‘-’表示不一致，所以要用DateTime
            String dayFormat = DateUtil.formatDate(day); //转为2025-3-11format格式查询
            //获取当天所有发布的文章的数量
            Integer count = articleService.selectCountByDate(dayFormat);
            countList.add(count);
        }
        map.put("count",countList);  //y轴发布文章数量
        return Result.success(map);
    }

    @GetMapping("/pieData")
    public Result getPieData() {
        List<Map<String,Object>> list = new ArrayList<>();
        List<Employee> employeeList=  employeeService.selectAll(null);
        Set<String> departmentNameSet = employeeList.stream().map(Employee::getDepartmentName).collect(Collectors.toSet()); //toset去重
        for (String departmentName : departmentNameSet) {
            HashMap<String,Object> map = new HashMap<>();
            map.put("name",departmentName);
            //统计这个部门下面的员工数量
            Long count = employeeList.stream().filter(employee -> employee.getDepartmentName().equals(departmentName)).count();
            map.put("value",count);
            list.add(map);
        }
        return Result.success(list);
    }

}//提交代码测试02
